/**
 * 視圖中介器, 负责loading的控制
 * 
 * @author LuoDong
 * @email luo4521@online.net.tw
 * 2010-1-29 9:52
 */
package com.addcn.texas.view
{

	import flash.display.*;
	import flash.events.Event;
	import flash.display.Stage;	
	
	import com.addcn.fog.view.*;
	import com.addcn.fog.control.*;
	import com.addcn.fog.model.*;
	
	import com.addcn.texas.Main;
	import com.addcn.texas.control.*
	import com.addcn.texas.view.*;
	import com.addcn.texas.model.*;
	
	import com.addcn.common.utils.AppLibary;
		
	public class LoadingView extends View
	{		
		public static const HIDE_LOADING : String = "hideLoading";
		public static const SHOW_LOADING : String = "showLoading";
		
		protected var _container : Object;
		private var _app : AppLibary = AppLibary.getInstance();	
		private var _loading_mc : Object;
		private var _class : Class;
		
		public function LoadingView(r_target : Object) {
			_container = r_target;		
			
		}			
		
		/**
		 * 該視圖中介器需要偵聽的通知
		 */
		override public function notificationsList():void 
		{
			notifications = [
							Main.APP_STARTUP,							
							SHOW_LOADING,
							HIDE_LOADING,
							];
		}
		
		/**
		 * 收到通知后,要執行的函數
		 * 
		 * @param	notification
		 * @param	data
		 */
		override public function onDataChanged(notification:String, data:Object = null):void {
			//根據收到的 notification 類型, 調用函數			
			switch (notification) {
				case Main.APP_STARTUP:
				case SHOW_LOADING:
					_showLoading();
					break;	
				case HIDE_LOADING:
					_hideLoading();
					break;
			}			
		}
		
		private function _showLoading() : void {	
			if ( _loading_mc != null) return;
			sendNotification(PopupView.SHOW_BECLOUD);
			_class = _class ==null ? _app.getClass("CycleLoading") : _class;
			if( _class != null){
				this._loading_mc  = new _class;
				this._container.addChild(_loading_mc);				
				this._loading_mc.x = (this._container.stage.stageWidth - _loading_mc.width ) * 0.5;
				this._loading_mc.y = (this._container.stage.stageHeight - _loading_mc.height ) * 0.5;
			}
		}
		
		private function _hideLoading() :void {
			if(this._loading_mc){
				this._container.removeChild(this._loading_mc);		
			}
			sendNotification(PopupView.HIDE_BECLOUD);
			this._loading_mc = null;
		}
		
	}
	
}
